package kalambury.srv.web;

import kalambury.db.UserEntity;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class UserValidator implements Validator{

    @Override
    public boolean supports(Class clazz) {
        return UserEntity.class.isAssignableFrom(clazz);
    }

    public void validatePage1Form(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username",
                "required.username", "Login jest wymagany.");
    }

    public void validatePage2Form(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
                "required.password", "Haslo jest wymagane.");
    }


    @Override
    public void validate(Object target, Errors errors) {
        validatePage1Form(target, errors);
        validatePage2Form(target, errors);
    }
}